#ifndef T_MY_HTTPBACKEND_P_H
#define T_MY_HTTPBACKEND_P_H

#include "private/qnetworkaccessbackend_p.h"

#include "networkaccessmanager.h"

class T_HttpReply;

class T_MyBackendFactory : public NetworkAccessBackendFactory
{
    Q_OBJECT
public:
    T_MyBackendFactory(QObject * parent = 0) : NetworkAccessBackendFactory(parent)
    {
    }
    //virtual QObject *create(QNetworkAccessManagerPrivate * manager,
    virtual QObject *create(QNetworkAccessManager * manager,
                            QNetworkAccessManager::Operation op,
                            const QNetworkRequest & request,
                            QIODevice * outgoingData) const;
};

class T_MyHttpBackend: public QNetworkAccessBackend
{
    Q_OBJECT
public:
    explicit T_MyHttpBackend(QNetworkAccessManager * manager);
	virtual ~T_MyHttpBackend();

	virtual void open();
	virtual void downstreamReadyWrite();
	// this indicates that the user closed the stream while the reply isn't finished yet
	virtual void closeDownstreamChannel() {}
	// HTTP backend does not support waitForReadyRead()
	virtual bool waitForDownstreamReadyRead(int msecs) { return false; }
	// we return true since HTTP needs to send PUT/POST data again after having authenticated
	virtual bool needsResetableUploadData() { return true; }
	virtual void copyFinished(QIODevice *dev);
    // SSL
    virtual void ignoreSslErrors();
    virtual void ignoreSslErrors(const QList<QSslError> & errors);
    virtual void fetchSslConfiguration(QSslConfiguration & config) const;
    virtual void setSslConfiguration(const QSslConfiguration & config);

private slots:
	void finished(); // override
	void namAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator);
	void replyAuthenticationRequired(QAuthenticator *authenticator);
	void replyReadyRead();
    void replyFinished();
    void replyMetaDataChanged();
	void replyError(QNetworkReply::NetworkError errorCode);
	void replyUploadProgress(qint64 bytesSent, qint64 bytesTotal);
	void replyDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);

private:
	T_HttpReply *m_httpReply;
    ////QNetworkReply *m_headReply;
    QIODevice * m_uploadDevice;
#ifndef QT_NO_OPENSSL
    QSslConfiguration *m_pendingSslConfiguration;
    bool m_pendingIgnoreAllSslErrors;
    QList<QSslError> m_pendingIgnoreSslErrorsList;
#endif

	void p_disconnectFromHttp();
	void p_readFromHttp();
	void p_checkForRedirect(const int statusCode);
	void p_checkForRedirect_YouTube(QUrl url);
};

#endif
